/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myftp.app.model;

import java.io.*;

/**
 *
 * @author CAVARLE Glenn, LUCAS Stéphane
 * Model pour les serveurs de transfert
 */
public class ServerTransferModel {

    private ServerModel server = null;
    private ServerModel transferServer = null;
    private TransfertTableModel transferTable;
    private TransferType transferType;
    private String filname;
    private String path;

    public enum TransferType {

        DOWNLOAD,
        UPLOAD;

        public boolean isDownload() {
            return this == TransferType.DOWNLOAD;
        }

        public boolean isUpload() {
            return this == TransferType.UPLOAD;
        }
    }

    public ServerModel getParentServer() {
        return this.server;
    }

    public ServerModel getTransferServer() {
        return this.transferServer;
    }

    public void setServer(ServerModel server) {
        this.server = server;

    }

    public ServerTransferModel(ServerModel server, TransfertTableModel transferTable) throws IOException {
        this.server = server;
        this.transferTable = transferTable;

        this.transferServer = new ServerModel(server.getInetAddress().getHostName(), server.getPort());
        this.transferServer.login(server.getLogin(), server.getPasswd());
        this.transferServer.changeDirectory(server.getCurrentDir());
        this.transferServer.setConnectIndex(this.transferTable.getRowCount());
        this.transferServer.resetReceptionBuffer();
        this.transferServer.setPendingDlFile(server.getPendingDlFile());


    }

    public boolean upload() throws FileNotFoundException, IOException {
        return this.upload(this.path, this.filname);
    }

    public boolean upload(String path, String filename) throws FileNotFoundException, IOException {
        this.transferType = TransferType.UPLOAD;
        this.filname = filename;
        this.path = path;
        boolean result = this.transferServer.upload(path, filename);
        this.getParentServer().setPendingDlFile(this.getTransferServer().getPendingDlFile());
        return result;
    }

    public boolean download() throws FileNotFoundException, IOException {
        return this.download(this.path, this.filname);
    }

    public boolean download(String path, String filename) throws FileNotFoundException, IOException {

        this.transferType = TransferType.DOWNLOAD;
        this.filname = filename;
        this.path = path;
        boolean result = this.transferServer.download(path, filename);
        this.getParentServer().setPendingDlFile(this.getTransferServer().getPendingDlFile());
        return result;
    }

    public void setTransferTable(TransfertTableModel transfertsTable) {
        this.transferTable = transfertsTable;
    }

    public void deleteBrokenFile(String filename) {
        try {
            this.transferServer.deleteFile(filename);
        } catch (IOException ex) {
        }
    }

    public boolean isUpload() {
        return this.transferType.isUpload();
    }

    public boolean isDownload() {
        return this.transferType.isDownload();
    }

    public String getFilname() {
        return this.filname;
    }

    public String getPath() {
        return this.path;
    }
    
}
